<template>
  <div>
    <h2>会员信息添加页面</h2>
    <table border="1">
      <tr>
        <td>会员真实姓名</td>
        <td>
          <input type="text" v-model="state.m_Name">
        </td>
      </tr>
      <tr>
        <td>会员手机号</td>
        <td>
          <input type="text" v-model="state.m_Phone">
        </td>
      </tr>
      <tr>
        <td>会员身份证号</td>
        <td>
          <input type="text" v-model="state.m_Card">
        </td>
      </tr>
      <tr>
        <td>会员卡号</td>
        <td>
          <input type="text" v-model="state.m_Code">
        </td>
      </tr>
      <tr>
        <td>会员等级</td>
        <td>
          <select v-model="state.m_Grape">
            <option value="">--请选择--</option>
            <option value="1">初级</option>
            <option value="2">中级</option>
            <option value="3">高级</option>
          </select>
        </td>
      </tr>
      <tr>
        <td>消费金额</td>
        <td>
          <input type="text" v-model="state.m_Price">
        </td>
      </tr>
      <tr>
        <td>地址</td>
        <td>
          <input type="text" v-model="state.m_Addr">
        </td>
      </tr>
      <tr>
        <td></td>
        <td>
          <input type="button" value="添加" @click="add">
        </td>
      </tr>
    </table>
  </div>
</template>

<script setup lang="ts">
import { reactive,toRefs } from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';
const router = useRouter()

const data = reactive({
  state:{
    m_Name:'',
    m_Phone:'',
    m_Card:'',
    m_Code:'',
    m_Grape:'',
    m_Addr:'',
    m_Price:''
  }
})

let {state} = toRefs(data)

const add=()=>{
  if(state.value.m_Name == ""){
    return alert("真实姓名不能为空");
  }
  if(state.value.m_Phone == ""){
    return alert("手机号不能为空");
  }
  if(state.value.m_Card.length <18 || state.value.m_Card.length > 18){
    return alert("身份证号必须是18为")
  }
  axios({
    url:'/api/Member/PostMemberAdd',
    method:'post',
    data:state.value
  })
  .then(res=>{
    console.log(res)
    if(res.data.data == -1){
      return alert("手机号已存在");
    }
    else if(res.data.data == 1){
      alert("添加成功");
      router.push('./memberlist')
    }
    else{
      alert("添加失败");
      return
    }
  })
  .catch(err=>{
    console.log(err)
  })
}
</script>

<style scoped>

</style>